$(function() {
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/FeedBack/getDatas/"+$('.tab-item-check').data('feedback_type')+'/'+$('#role').val(),
		paramFunc: paramFunc,
		operation: {
			"icon-plus-sign-alt": [function(index, trData,element) {
				// alert(index);
				if (element.parents("tr").next("tr").hasClass("XRappend" + index)) {
					$("#table tbody tr.XRappend" + index).each(function() {
						$(this).remove();
						$('.PIDappend'+index).remove();
					})
					element.parents("tr").find('.icon-plus-sign-alt').removeClass('icon-spin').removeClass('icon-large');
					return;
				}
				element.parents("tr").find('.icon-plus-sign-alt').addClass('icon-spin').addClass('icon-large');
				var insertRow = $("<tr class='XRappend" + index + " detail-row open' style='display:none;'></tr>");

				//数据插入demo
				$('.sub-demo .result').html(trData.result);
				$('.sub-demo .content').html(trData.content);
				$('.sub-demo .save-result').attr('onclick','writeResult(this)').attr('data-id',trData.id);
				//result可编辑性
				if(trData.can_write){
					$('.sub-demo .result').removeAttr('readonly');
				}
				else{
					$('.sub-demo .result').attr('readonly','readonly');
				}

				var html='<div class="col-md-12" style="background-color:#f7f9fa; padding:10px!important;"><div class="col-md-12" style="background-color:#fff!important;">'+$('.sub-demo').html()+'</div></div>';

				insertRow.append('<td colspan="10"><div>'+html+'</div></td>');	
				

				element.parents("tr").after(insertRow);
				
				$(".XRappend" + index).find('.show-img').click(function(){
					showImg(trData.img);
				});
				$(".XRappend" + index).fadeIn(200);
			}, "详细信息"],
			"icon-list": [showRecord, "反馈操作记录"],
			"icon-group": [submitToManage, "提交至管理员"],
			"icon-share": [assignDealAdmin, "转发至指定用户"],
			"icon-print": [doEnd, "结束"],
			"icon-random": [cancelEnd, "取消结束"],
			"icon-refresh": [setShow, "校领导可见/不可见"],
			// "icon-refresh": [changeStatus, "启用/冻结"]
		},
		hasCheckbox: false,
		afterFunc:afterFunc
	});

	// exportEvent();
	// 
	$('.tab-item').click(function(){
		$('.tab-item').removeClass('tab-item-check');
		$(this).addClass('tab-item-check');
		$('#table').resetAjaxUrl("Admin/FeedBack/getDatas/"+$('.tab-item-check').data('feedback_type')+'/'+$('#role').val());
		$("#table").search();
	});
});

var paramFunc = function() {
	var data = getFormJson();
	return data;
};

var afterFunc=function(){
	$("#table tbody tr").each(function() {
        var data = $("#table").getOpts().datas[$(this).index()];

        if(!data.can_end){
        	$(this).children("td:nth-child(2)").find(".icon-print").hide();
        }
        if(!data.can_cancel_end){
        	$(this).children("td:nth-child(2)").find(".icon-random").hide();
        }
        if(!data.can_submit_to_manage){
        	$(this).children("td:nth-child(2)").find(".icon-group").hide();
        }
        if(!data.can_assign_deal_admin){
        	$(this).children("td:nth-child(2)").find(".icon-share").hide();
        }
        if(!data.can_show){
        	$(this).children("td:nth-child(2)").find(".icon-refresh").hide();
        }
    });
}



var showRecord = function(idx, trData) {
	showDialogModal(getRootPath()+'/Admin/FeedBack/showRecord/'+$('.tab-item-check').data('feedback_type')+'/'+trData.id);
};


var doEnd = function(idx, trData) {
	var msg = "是否确认结束该反馈";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/FeedBack/doEnd/"+$('.tab-item-check').data('feedback_type')+'/'+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};
var cancelEnd = function(idx, trData) {
	var msg = "是否确认取消结束该反馈";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/FeedBack/cancelEnd/"+$('.tab-item-check').data('feedback_type')+'/'+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};

var submitToManage = function(idx, trData) {
	var msg = "是否确认将该反馈提交至管理员";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/FeedBack/submitToManage/"+$('.tab-item-check').data('feedback_type')+'/'+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};

var setShow = function(idx, trData) {
	if(trData.show==1){
		var msg = "是否确认将该反馈设置为领导不可见";
	}
	else{
		var msg = "是否确认将该反馈设置为领导可见";
	}
	
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/FeedBack/setShow/"+$('.tab-item-check').data('feedback_type')+'/'+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};

var assignDealAdmin = function(idx, trData) {
	//两步，先取下拉列表
	var load_alert=loadAlert('正在获取转发队列...');
	$.ajax({
		type: "post",
		url: "Admin/FeedBack/getAdminList/"+$('.tab-item-check').data('feedback_type')+'/'+trData.id,
		async: true,
		dataType:'json',
		success: function(data) {
			closeAlert(load_alert);
			if(data.status) {
				var html='<div style="height:200px;"><select class="belong"><option value="0">请选择...</option>';
				var t=JSON.parse(data.v);
				$.each(t,function(k,v){
					html+='<option value="'+v.id+'">'+v.username+'（'+v.usercode+'）'+'</option>'
				});
				html+='</select></div>';

				showDialogModalConfirm('confirm','请选择',html+'<script>$(".belong").searchableSelect();</script>' ,'确认','取消',function(){ 
				    if($('.belong').val()==0){
				    	window.parent.topMessagePrompt('error','请选择用户');
				    }else{
				    	var load_alert2=loadAlert('转发中...');
				    	$.ajax({
				    		type: "post",
				    		url: "Admin/FeedBack/assignDealAdmin/"+$('.tab-item-check').data('feedback_type')+'/'+trData.id,
				    		async: true,
				    		data:{belong:$('.belong').val()},
				    		dataType:'json',
				    		success: function(data) {
				    			closeAlert(load_alert2);
				    			if(data.status) {
				    				window.parent.topMessagePrompt('success',data.msg);
				    				window.parent.refreshTable();
				    			} else {
				    				window.parent.topMessagePrompt('error',data.msg);
				    			}
				    		}
				    	});
				    }
				});
			} else {
				window.parent.topMessagePrompt('error',data.msg);
			}
		}
	});
};



function writeResult(obj){
	var id=$(obj).data('id');
	var result=$(obj).parents('.demo-content').find('.result').val();
	var load_alert=loadAlert('保存中，请稍后...');
	$.ajax({
		type: "post",
		url: "Admin/FeedBack/writeResult/"+$('.tab-item-check').data('feedback_type')+'/'+id,
		async: true,
		data:{
			result:result
		},
		success: function(data) {
			closeAlert(load_alert);
			var ret = $.parseJSON(data);
			if(ret.status) {
				window.parent.topMessagePrompt('success',ret.msg);
	            window.parent.refreshTable();
			} else {
				window.parent.topMessagePrompt('error',ret.msg);
			}
		}
	});	
}


function showImg(img){
	var new_img={data:[]}
	$.each(img,function(k,v){
		new_img.data.push({'alt':'','pid':v.id,'src':v.img,'thumb':''});
	});
	showDialogModalPhoto(new_img);
}